// --------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------
// Module: Breath_led
// 
// Author: Step
// 
// Description: PWM-Breath_led
// 
// Web: www.stepfpga.com
//
// --------------------------------------------------------------------
// Code Revision History :
// --------------------------------------------------------------------
// Version: |Mod. Date:   |Changes Made:
// V1.0     |2015/11/11   |Initial ver
// --------------------------------------------------------------------
module breath_led 
(
input		clk,			//system clk
input		rst_n,		//system reset
output		breath_led		//Breath led output
);

parameter	CNT_NUM = 3464;	//period = (3464**2)*2 = 24_000_000(12M*2) = 2s
//parameter	CNT_NUM = 2449;	//period = (2449^2)*2 = 12_000_000 = 2s

reg	[12:0]	cnt1;
//generate cnt1 signal
always@(posedge clk or negedge rst_n) begin 
	if(!rst_n) begin
		cnt1<=13'd0;
	end else begin
		if(cnt1>=CNT_NUM-1) cnt1<=1'b0;
		else cnt1<=cnt1+1'b1;
	end
end

reg	dir_cnt2;
reg	[12:0]	cnt2;
//generate cnt2 signal
always@(posedge clk or negedge rst_n) begin 
	if(!rst_n) begin
		cnt2<=13'd0;
		dir_cnt2<=1'b0;
	end else begin
		if(cnt1==CNT_NUM-1) begin
			if(!dir_cnt2) begin
				if(cnt2>=CNT_NUM-1) dir_cnt2<=1'b1;
				else cnt2<=cnt2+1'b1;
			end else begin
				if(cnt2<=0) dir_cnt2<=1'b0;
				else cnt2<=cnt2-1'b1;
			end
		end else cnt2<=cnt2;
	end
end

//Compare cnt1 and cnt2, generate PWM-Breath-led
assign	breath_led = ((cnt1+CNT_NUM/5)<cnt2)?1'b0:1'b1;
	
endmodule
	